﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MarkdEdit
{

    static class Program
    {
        /// <summary>
        ///  The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.ApplicationExit += Application_ApplicationExit;
            Application.ThreadException += Application_ThreadException;
            AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
            //Application.SetHighDpiMode(HighDpiMode.SystemAware);
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Log.LogFactory.GetLogger().Info("程序启动");
            Application.Run(new MainForm());
            Log.LogFactory.GetLogger().Info("主线程退出");
        }

        private static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
        {
            Log.LogFactory.GetLogger().Error("Application_ThreadException", e.Exception);
            var ex = e.Exception;
            MessageBox.Show(ex.Message + "\n" + ex.StackTrace, "Thread Exception");
        }

        private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
        {
            if (e != null && e.ExceptionObject is Exception)
            {
                var ex = e.ExceptionObject as Exception;
                Log.LogFactory.GetLogger().Error("UnhandledException::", ex);
                MessageBox.Show(ex.Message + "\n" + ex.StackTrace, "Unhandled Exception");
            }
            else
            {
                Log.LogFactory.GetLogger().Error("UnhandledException::" + e.ToString());
            }
        }

        private static void Application_ApplicationExit(object sender, EventArgs e)
        {
            Log.LogFactory.GetLogger().Info("应用程序退出");
        }
    }

}
